from marshmallow import EXCLUDE
from webargs import core
from webargs.multidictproxy import MultiDictProxy


class BareParser(core.Parser):
    """
    from webargs import fields
    @use_args({'name': fields.Str(required=True)})
    def test(args, data):
        return args['name']

    test({'name': "hello"})
    """
    DEFAULT_LOCATION = 'form'
    DEFAULT_UNKNOWN_BY_LOCATION = {'form': EXCLUDE}

    def load_form(self, req, schema):
        return MultiDictProxy(req, schema)

    def get_request_from_view_args(self, view, args, kwargs):
        # first argument is self
        return args[1]

    def _get_schema(self, argmap, req):
        schema = super()._get_schema(argmap, req)
        schema.unknown = EXCLUDE
        return schema

    @staticmethod
    def _update_args_kwargs(args, kwargs, parsed_args, as_kwargs):
        """Update args or kwargs with parsed_args depending on as_kwargs"""
        if as_kwargs:
            kwargs.update(parsed_args)
        else:
            # replace origin data args
            args = list(args)
            args[1] = parsed_args
        return args, kwargs


parser = BareParser()
use_args = parser.use_args
